package com.example.administrator.ygddriver.netty;

import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

import com.baidu.location.BDAbstractLocationListener;
import com.baidu.location.BDLocation;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.example.administrator.ygddriver.base.LogInformationBean;
import com.example.administrator.ygddriver.base.UserBean;
import com.example.administrator.ygddriver.listen.ListenFragment;
import com.example.administrator.ygddriver.liteorm.LiteOrmTools;
import com.example.administrator.ygddriver.liteorm.SingleLiteOrm;
import com.example.administrator.ygddriver.tool.MyApp;
import com.example.administrator.ygddriver.tool.UrlValuse;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

public class SimpleChatClientHandler extends SimpleChannelInboundHandler<String> {
    private String city;
    private LocationClient mLocationClient = null;
    private MyLocationListener myListener = new MyLocationListener();
    private String c_account;
    //客户端账号唯一标识;

    //private static final Logger logger = LoggerFactory.getLogger(TcpClientHandler.class);

    protected void channelRead0(ChannelHandlerContext ctx, String s) {
        mLocationClient = new LocationClient(MyApp.context);
        //声明LocationClient类
        mLocationClient.registerLocationListener(myListener);
        //注册监听函数
        initOption();

        String[] context = s.split(",");
        if (context[1].equals("7")) {
            Intent intent = new Intent();
            intent.putExtra("num", context[2]);
            intent.setAction(UrlValuse.QR_JJ);
            MyApp.context.sendBroadcast(intent);

        }
        if (context[1].equals("3")) {
            Intent intent = new Intent();
            intent.setAction(UrlValuse.GET_NEW_BILL);
            MyApp.context.sendBroadcast(intent);
        }
        if (context[1].equals("2")) {
            Intent intent = new Intent();
            intent.setAction(UrlValuse.GET_NEW_BILL);
            MyApp.context.sendBroadcast(intent);
        }
        if (context[1].equals("1")) {
            Intent intent = new Intent();
            intent.setAction(UrlValuse.GET_NEW_BILL);
            MyApp.context.sendBroadcast(intent);
        }

        if (context[1].equals("8")) {
            c_account = context[2];
            mLocationClient.start();

        }
        if (context[1].equals("4")) {
            Intent intent = new Intent();
            intent.setAction(UrlValuse.GET_NEW_BILL);
            MyApp.context.sendBroadcast(intent);
        }
        if (context[1].equals("5")) {
            Intent intent = new Intent();
            intent.setAction(UrlValuse.GET_NEW_BILL);
            MyApp.context.sendBroadcast(intent);
        }

        Log.d("1111111111111111", s + "*****************");

    }

    @Override
    public void channelActive(final ChannelHandlerContext ctx) throws Exception {
        super.channelActive(ctx);
        String channelId = ctx.channel().id().toString();

        System.out.println("---------------Channel:" + channelId + " 连接成功-----------------");
//         通道建立(c_account 账号唯一标识 c_type 应用类型 c_operation 操作类型)
        LiteOrmTools.getInformation(LogInformationBean.class, MyApp.context, new LiteOrmTools.On<LogInformationBean>() {
            @Override
            public void hasData(LogInformationBean data) {
                city = data.getC_city();
            }
        }, new LiteOrmTools.OFF() {
            @Override
            public void noData() {

            }
        });
        LiteOrmTools.getInformation(UserBean.class, MyApp.context, new LiteOrmTools.On<UserBean>() {
            @Override
            public void hasData(UserBean data) {
                String msg0 = "{c_account:" + data.getC_id() + ",c_type:2,c_operation:0,c_city:" + city + " }&";
                ctx.writeAndFlush(msg0);
            }
        }, new LiteOrmTools.OFF() {
            @Override
            public void noData() {

            }
        });

//        // 开始听单(c_account 司机端账号唯一标识 c_city 服务城市 c_operation 操作类型)
//        String msg1 = "{\"c_account\":\"123\",\"c_city\":\"沈阳市\",\"c_operation\":\"1\"}";
//        // 停止听单(c_account 司机端账号唯一标识 c_city 服务城市 c_operation 操作类型)
//        String msg2 = "{\"c_account\":\"123\",\"c_city\":\"沈阳市\",\"c_operation\":\"2\"}";
//        // 发布订单(c_waybill 订单唯一标识 c_valuation 计价方式 c_weight 重量 c_city 所在城市c_operation 操作类型 )
//        String msg3 = "{\"c_waybill\":\"123\",\"c_valuation\":\"2\",\"c_weight\":\"2\",\"c_city\":\"沈阳市\",\"c_operation\":\"3\"}";
//        // 托管订单(c_waybill 订单唯一标识  c_operation 操作类型)
//        String msg4 = "{\"c_waybill\":\"123\",\"c_operation\":\"4\"}";
//        // 取消托管(c_waybill 订单唯一标识  c_operation 操作类型)
//        String msg5 = "{\"c_waybill\":\"123\",\"c_operation\":\"5\"}";
//        // 抢单(可议价 c_account 用户端账号唯一标识  c_operation 操作类型)
//        String msg6 = "{\"c_account\":\"123\",\"c_operation\":\"6\"}";
//        // 呼起订单 (c_waybill 订单号 c_operation 操作类型)
//        String msg7 = "{\"c_waybill\":\"123\",\"c_operation\":\"7\"}";

    }

    public class MyLocationListener extends BDAbstractLocationListener {
        @Override
        public void onReceiveLocation(BDLocation location) {
            //此处的BDLocation为定位结果信息类，通过它的各种get方法可获取定位相关的全部结果
            //以下只列举部分获取经纬度相关（常用）的结果信息
            //更多结果信息获取说明，请参照类参考中BDLocation类中的说明

            final double latitude = location.getLatitude();    //获取纬度信息
            final double longitude = location.getLongitude();    //获取经度信息
            int errorCode = location.getLocType();
            //获取定位类型、定位错误返回码，具体信息可参照类参考中BDLocation类中的说明
            if (errorCode == 161 || errorCode == 61) {
                LiteOrmTools.getInformation(UserBean.class, MyApp.context, new LiteOrmTools.On<UserBean>() {
                    @Override
                    public void hasData(UserBean data) {
                        String msg9 = "{c_account:" + c_account + ",c_weight:" + String.valueOf(latitude) + ",c_city:" + String.valueOf(longitude) + ",c_operation:9,c_waybill:" + data.getC_id() + "}&";
                        TcpClient instance_t = TcpClient.getInstance();
                        instance_t.sendMsgByByete(msg9);
                    }
                }, new LiteOrmTools.OFF() {
                    @Override
                    public void noData() {

                    }
                });


                mLocationClient.stop();
            } else {
            }

        }
    }

    private void initOption() {
        LocationClientOption option = new LocationClientOption();
        option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);
        option.setCoorType("bd09ll");
        option.setScanSpan(1000);
        option.setOpenGps(true);
        option.setLocationNotify(true);
        //可选，设置是否当GPS有效时按照1S/1次频率输出GPS结果，默认false
        option.setIgnoreKillProcess(true);
        //可选，定位SDK内部是一个service，并放到了独立进程。
        //设置是否在stop的时候杀死这个进程，默认（建议）不杀死，即setIgnoreKillProcess(true)
        option.SetIgnoreCacheException(false);
        //可选，设置是否收集Crash信息，默认收集，即参数为false
        option.setWifiCacheTimeOut(5 * 60 * 1000);
        //可选，V7.2版本新增能力
        //如果设置了该接口，首次启动定位时，会先判断当前Wi-Fi是否超出有效期，若超出有效期，会先重新扫描Wi-Fi，然后定位
        option.setEnableSimulateGps(false);
        //可选，设置是否需要过滤GPS仿真结果，默认需要，即参数为false
        mLocationClient.setLocOption(option);
    }


}
